package com.lms.controller;


import com.lms.bean.Book;
import com.lms.bean.Role;
import com.lms.bean.Type;
import com.lms.bean.User;
import com.lms.service.RoleService;
import com.lms.service.UserService;
import com.lms.util.MyStringUtils;
import com.lms.util.QueryResult;
import com.lms.util.ResultUtils;
import com.lms.vo.BookVO;
import com.lms.vo.UserVO;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("admin/user-list")
    public String toUserList(Model model) {
        List<Role> roleList = roleService.findAll();
        model.addAttribute("roleList", roleList);
        return "admin/user-list";
    }

    @RequestMapping("admin/user-input")
    @RequiresPermissions(value = {"user-update","user-add"},logical = Logical.OR)
    public String toUserInput(Long userId, Model model) {
        if(-1==userId){
            model.addAttribute("operation","添加用户");
            model.addAttribute("user",new User());
        }else{
            model.addAttribute("operation","更新用户");
            User user = userService.findUserById(userId);
            model.addAttribute("user",user);
        }
        List<Role> roles = roleService.findAll();
        model.addAttribute("roles",roles);
        return "admin/user-input";
    }
    @RequestMapping("save")
    @RequiresPermissions(value = {"user-update","user-add"},logical = Logical.OR)
    public String save(User user){
        userService.save(user);
        return "redirect:admin/user-list";
    }

    /**
     * @param pageFromLayUI  计数方式是 1,2,3,4...,n  默认为 1 ;
     *                       但是 Pageable.page 的计数方式是 0,1,2,3,...,n-1 默认为 0
     * @param limitFromLayUI 等于 Pageable.sizei
     * @return 能被 layUI 接受的 JSON, 格式为 { "code": 0, "msg": "OK", count": 14, "data":[{},{},{}] }
     */
    @RequestMapping("all")
    @ResponseBody
    public ResultUtils<UserVO> findAll(@RequestParam(value = "page", defaultValue = "1") Integer pageFromLayUI,
                                       @RequestParam(value = "limit", defaultValue = "10") Integer limitFromLayUI,
                                       @RequestParam(value = "queryParam[userId]", defaultValue = "") String userId,
                                       @RequestParam(value = "queryParam[keyWord]", defaultValue = "") String keyWord,
                                       @RequestParam(value = "queryParam[roleId]", defaultValue = "-1") String roleId,
                                       @RequestParam(value = "queryParam[queryType]", defaultValue = "1") String queryType) {
         //0精确搜索

        if ("0".equals(queryType)) {
            //精确查询
            if (MyStringUtils.isNumeric(userId)) {
                //传入的用户编号是数字
                User user = userService.findUserById(Long.parseLong(userId));
                if (null != user) {
                    UserVO userVO = UserVO.convert(user);
                    List<UserVO> userVOList = new ArrayList<>();
                    userVOList.add(userVO);
                    return ResultUtils.layUITable(1L, userVOList);
                }
            }
            //传入的图书编号不是数字 或者 null==book
            return ResultUtils.layUITable(0L, new ArrayList<>());

        } else {
            //模糊查询
            User param = new User();
            if(null==keyWord){
                keyWord ="";
            }
            String s = StringUtils.trimAllWhitespace(keyWord);
            param.setUsername(s);
            QueryResult<UserVO> queryResult;
            if ("-1".equals(roleId) || !MyStringUtils.isNumeric(roleId)) {
                //不限用户权限
                queryResult = userService.queryByKeyWordLikeNoType(pageFromLayUI - 1, limitFromLayUI, param);
            } else {
                //限制图书类别
                queryResult = userService.queryByKeyWordLikeWithType(pageFromLayUI - 1, limitFromLayUI, param, Long.parseLong(roleId));
            }
            //返回JSON结果
            return ResultUtils.layUITable(queryResult.getTotalCount(), queryResult.getData());
        }
    }
    @RequestMapping("delete")
    //@RequiresRoles(value = "superadmin")
    @RequiresPermissions({"user-delete"})
    public String deleteById(Long userId){
        userService.deleteById(userId);
        return "redirect:admin/user-list";
    }

    @RequestMapping("deleteAll")
    @RequiresPermissions({"user-delete"})
    //@RequiresRoles(value = "superadmin")
    public String deleteById(String ids){
        userService.deleteAll(ids);
        return "redirect:admin/user-list";
    }

}